package com.flycent.hqhs.ui;

import java.util.ArrayList;
import java.util.List;

import com.flycent.hqhs.R;
import com.flycent.hqhs.bean.ProductBundle;
import com.flycent.hqhs.bean.ProductGroup;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

/**
 * @author fbl
 *  促销 
 */
public class ProductPromotionsActivity extends BackableTitleBarActivity{
	private Fragment[] mFragments;
	private RadioGroup bottomRg;
	private FragmentManager fragmentManager;
	private FragmentTransaction fragmentTransaction;
	@SuppressWarnings("unused")
	private RadioButton rbOne, rbTwo;
	public static List<ProductBundle> mBundles = new ArrayList<ProductBundle>();
	public static List<ProductGroup> mGroups = new ArrayList<ProductGroup>();

	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_product_promotion);
		getTitleBar().setTitle(R.string.text_product_promotion);
		
		Gson gson = new Gson();
		
		String mProductBundles = getIntent().getStringExtra("mProductBundles");
		String mProductGroups  = getIntent().getStringExtra("mProductGroups");
		
		mBundles = gson.fromJson(mProductBundles, new TypeToken<List<ProductBundle>>(){}.getType());
		mGroups = gson.fromJson(mProductGroups, new TypeToken<List<ProductGroup>>(){}.getType());
		
		mFragments = new Fragment[2];
		fragmentManager = getSupportFragmentManager();
		mFragments[0] = fragmentManager.findFragmentById(R.id.fragment_product_package);
		mFragments[1] = fragmentManager.findFragmentById(R.id.fragment_product_combination);
		fragmentTransaction = fragmentManager.beginTransaction()
				.hide(mFragments[0]).hide(mFragments[1]);
		
		if( mBundles != null ){
			fragmentTransaction.show(mFragments[0]).commit();
		}else {
			fragmentTransaction.show(mFragments[1]).commit();
		}
		
		
		setFragmentIndicator();

	}


	private void setFragmentIndicator() {

		bottomRg = (RadioGroup) findViewById(R.id.product_promotion_radioGroup);
		rbOne = (RadioButton) findViewById(R.id.product_promotion_radio0);
		rbTwo = (RadioButton) findViewById(R.id.product_promotion_radio1);
		
		if( mBundles != null ){
			bottomRg.check(R.id.product_promotion_radio0);
		}else {
			bottomRg.check(R.id.product_promotion_radio1);
		}

		bottomRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				fragmentTransaction = fragmentManager.beginTransaction()
						.hide(mFragments[0]).hide(mFragments[1]);
				switch (checkedId) {
				case R.id.product_promotion_radio0:
					fragmentTransaction.show(mFragments[0]).commit();
					break;

				case R.id.product_promotion_radio1:
					fragmentTransaction.show(mFragments[1]).commit();
					break;

				default:
					break;
				}
			}
		});
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);

	}
}
